/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package caintra.Sapra.ComponentsImplements;

import caintra.Sapra.Model.Calendariofacturacionpracticantes;
import caintra.Sapra.Pantalla.Empresa.EmpresaController;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.TooManyListenersException;
import nextapp.echo2.app.filetransfer.UploadEvent;
import nextapp.echo2.app.filetransfer.UploadListener;
import nextapp.echo2.app.filetransfer.UploadSelect;

/**
 *
 * @author luiz
 */
public class UploadCaintra{
    
    public EmpresaController control;
    public UploadCaintra(EmpresaController control){
        this.control=control;
    
    }
    public UploadSelect getUploadSelect() {
        UploadSelect uploadSelect = new UploadSelect();
        try {
            uploadSelect.addUploadListener(new UploadListener() {
                public void fileUpload(UploadEvent ue) {
                    try {
                        String nameFile=ue.getFileName();
                             String fechaStr="";
                             byte[] b = new byte[ue.getSize()];
                             ue.getInputStream().read(b, 0, ue.getSize());
                             File f=new File(nameFile);
                             f.createNewFile();
                             FileOutputStream outputStream = new FileOutputStream(f);
                             outputStream.write(b);
                             outputStream.close();
                             BufferedReader r=new BufferedReader(new FileReader(f));
                             r.readLine();
                             boolean bandera_fecha=false;
                             control.lista_fechas=new ArrayList<Calendariofacturacionpracticantes>();
                             while(true){
                                 try {
                                     fechaStr=r.readLine();
                                     if (fechaStr == null || fechaStr.trim().equals(""))
                                            break;
                                     else{
                                         bandera_fecha = control.Lista_Fecha(fechaStr);
                                         if(bandera_fecha == false){
                                             control.lista_fechas=new ArrayList<Calendariofacturacionpracticantes>();
                                             return;
                                         }
                                     }
                                         
                                 } catch (Exception e) {
                                     control.lista_fechas=new ArrayList<Calendariofacturacionpracticantes>();
                                 }
                             }
                             
                             r.close();
                             f.delete();
                             f.deleteOnExit();
                    } catch (Exception e) {
                    }
                }

                public void invalidFileUpload(UploadEvent uploadEvent) {
                    
                }
            });
        } catch (TooManyListenersException e) {
            // No registrar más de un observador...
        }
        return uploadSelect;
    }


    
}
